package com.sls.game.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.GoodsSupplyManageInfo;

/**
 * 
 * @description 商品供应表操作
 * @author Wangxuegang
 * @date 2012-9-18
 * 
 */
@Repository("goodsSupplyManageDAO")
public class GoodsSupplyManageDAOImpl extends SuperDAO<GoodsSupplyManageInfo>
		implements GoodsSupplyManageDAO {

	/**
	 * 分页查询
	 * 
	 * @param goodsSupplyManageInfo
	 * @return
	 */
	@Override
	public Pagination<GoodsSupplyManageInfo> queryPaginationList(
			GoodsSupplyManageInfo info) {
		Pagination<GoodsSupplyManageInfo> paginationList = null;
		Integer rowCount = queryRowCount(info);

		info.calculate();
		List<GoodsSupplyManageInfo> list = queryGoodsInfoList(info);
		paginationList = this.queryPagination(rowCount, list, info);

		return paginationList;
	}

	/**
	 * 查询记录总数
	 * 
	 * @param pageInruire
	 * @return
	 */
	private Integer queryRowCount(GoodsSupplyManageInfo info) {
		Integer rowCount = 0;
		rowCount = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsSupplyManageMapper.queryRowCount", info);
		return rowCount;
	}

	/**
	 * 记录结果集list
	 * 
	 * @param
	 * @return
	 */
	private List<GoodsSupplyManageInfo> queryGoodsInfoList(
			GoodsSupplyManageInfo info) {
		List<GoodsSupplyManageInfo> list = null;
		list = this.getSqlSession().selectList(
				"mybatis.xml.GoodsSupplyManageMapper.queryGoodsInfoList", info);
		return list;
	}

	/**
	 * 根据产品Id，查询进价最低的记录 com.sls.game.pojo.GoodsSupplyManageInfo
	 * 
	 * @param goodsId
	 */
	@Override
	public List<GoodsSupplyManageInfo> queryMininunPrice(
			GoodsSupplyManageInfo goodsSupplyManageInfo) {

		List<GoodsSupplyManageInfo> infoList = null;
		infoList = this.getSqlSession().selectList(
				"mybatis.xml.GoodsSupplyManageMapper.queryMininunPrice",
				goodsSupplyManageInfo);
		return infoList;
	}

	/**
	 * 商品插入
	 * 
	 * @param goodsSupplyManageInfo
	 */
	@Override
	public void insert(GoodsSupplyManageInfo goodsSupplyManageInfo) {
		this.getSqlSession().insert(
				"mybatis.xml.GoodsSupplyManageMapper.insert",
				goodsSupplyManageInfo);
	}

	/**
	 * 根据原始商品Id和供应商Id，查询单条记录
	 * 
	 * @param goodsSupplyManageInfo
	 * @return
	 */
	@Override
	public GoodsSupplyManageInfo queryByGoodsIdAndSupplyId(
			GoodsSupplyManageInfo goodsSupplyManageInfo) {
		GoodsSupplyManageInfo info = null;
		info = this
				.getSqlSession()
				.selectOne(
						"mybatis.xml.GoodsSupplyManageMapper.queryByGoodsIdAndSupplyId",
						goodsSupplyManageInfo);
		return info;
	}

	/**
	 * 商品更新
	 * 
	 * @param goodsId
	 */
	@Override
	public void update(GoodsSupplyManageInfo goodsSupplyManageInfo) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsSupplyManageMapper.update",
				goodsSupplyManageInfo);

	}

	/**
	 * 商品上下架操作
	 */
	@Override
	public void updateState(GoodsSupplyManageInfo goodsSupplyManageInfo) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsSupplyManageMapper.updateState2",
				goodsSupplyManageInfo);
	}
	
	/**
	 * 商品上下架操作
	 */
	@Override
	public void updateState2(GoodsSupplyManageInfo goodsSupplyManageInfo) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsSupplyManageMapper.updateState",
				goodsSupplyManageInfo);
	}

	/**
	 * 根据商品编号，查询该商品最高进价
	 * 
	 * @param goodsSupplyManageInfo
	 * @return
	 */
	@Override
	public String queryHighInPrice(GoodsSupplyManageInfo goodsSupplyManageInfo) {
		Object highPrice = null;
		String result = null;
		highPrice = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsSupplyManageMapper.queryHighInPrice",
				goodsSupplyManageInfo);
		if (highPrice != null) {
			result = highPrice.toString();
		}

		return result;
	}

	/**
	 * 根据手拉手商品Id，查询符合条件的记录
	 * 
	 * @param goodsSupplyManageInfo
	 * @return
	 */
	@Override
	public List<GoodsSupplyManageInfo> queryByGoodsId(
			GoodsSupplyManageInfo goodsSupplyManageInfo) {
		List<GoodsSupplyManageInfo> list = null;
		list = this.getSqlSession().selectList(
				"mybatis.xml.GoodsSupplyManageMapper.queryByGoodsId",
				goodsSupplyManageInfo);

		return list;
	}

	@Override
	public GoodsSupplyManageInfo queryByOriginalGoodsId(String originalGoodsId) {
		GoodsSupplyManageInfo supplyInfo = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsSupplyManageMapper.queryByOriginalGoodsId",
				originalGoodsId);
		return supplyInfo;
	}

	/**
	 * 根据平台商品编号和供应商Id，查询原始商品编号
	 * 
	 * @param info
	 * @return
	 */
	@Override
	public GoodsSupplyManageInfo queryGoodsSupplyInfo(GoodsSupplyManageInfo info) {
		GoodsSupplyManageInfo suppInfo = null;
		suppInfo = this.getSqlSession().selectOne(
				"mybatis.xml.GoodsSupplyManageMapper.queryGoodsSupplyInfo",
				info);
		return suppInfo;
	}

	@Override
	public List<String> querySupplyNameList(String goodsStatus) {
		List<String> supplyNames = this.getSqlSession().selectList(
				"mybatis.xml.GoodsSupplyManageMapper.querySupplyNameList",
				goodsStatus);
		return supplyNames;
	}

	@Override
	public void updateByGoodsId(GoodsSupplyManageInfo info) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsSupplyManageMapper.updateByGoodsId", info);
		
	}

	@Override
	public void updateByBrandId(GoodsSupplyManageInfo info) {
		this.getSqlSession().update(
				"mybatis.xml.GoodsSupplyManageMapper.updateByBrandId", info);
		
	}

}
